<?php

namespace Home\Controller;
use Home\Controller\CommonController;


class IndexController extends CommonController{
    /**
     * @首页数据字义
     */
    public function Index(){
        $params = I('get.');

        $token = $params['token'];
        $confstr = C('crypt_key');
        $mcrypt = new \Org\Util\Mcrypt();
        $this->assign('token',$token);
        $chektoken = $mcrypt::decode($token,$confstr);
        $userinfo = explode('+',$chektoken);
        $account = $userinfo[0];
        $accountid = $userinfo[1];
        $checksql = 'select * from spa_admin WHERE account="'.$account.'" and id='.$accountid;
        //验证是后台的管理员
        $checkuser = M()->query($checksql);
        if(empty($checkuser)){
//            $this->redirect('http://www.s2.com/admin',[],3,'非法操作,登录重试');
            header('Location:http://www.s2.com/admin');
            exit;
        }
        //验证是否有报表的权限
//        $reporturl = '/admin/newreport/index';
//        $admingroup = 'select group_id from spa_auth_group_access WHERE uid='.$accountid;
//        $group = M()->query($admingroup);
//        $groupid = $group[0]['group_id'];
//        $authgrouprule = 'select * from spa_auth_group WHERE status=1 AND id='.$groupid;
//        $grouprule = M()->query($authgrouprule);
//        $rules = $grouprule[0]['rules'];
//        $reportrulesql = 'select id from spa_auth_rule WHERE `name`= "'.$reporturl.'" and status=1';
//        $reportrule = M()->query($reportrulesql);
//        $reportruleid = $reportrule[0]['id'];
//
//        $rulesarr = explode(',',$rules);
//        if(!in_array($reportruleid,$rulesarr)){
////            $this->redirect('http://www.s2.com/admin',[],3,'非法操作,登录重试');
//            header('Location:http://www.s2.com/admin');
//            exit;
//        }

        //将用户SEESION赋值
        if(empty($_SESSION['userInfo']['id']))
            $_SESSION['userInfo']['id'] = $accountid;
        if(empty($_SESSION['userInfo']['realname']))
            $_SESSION['userInfo']['realname'] = $account;

        $regtime = empty($_SESSION['userInfo']['regtime'])?1487832088:$_SESSION['userInfo']['regtime'];
        $uid = empty($_SESSION['userInfo']['id'])?$accountid:$_SESSION['userInfo']['id'];
        $realname = empty($_SESSION['userInfo']['realname'])?$account:$_SESSION['userInfo']['realname'];
        $types = empty($_SESSION['userInfo']['types'])?1:$_SESSION['userInfo']['types'];
        
        //查询消息
        $where='where a.regtime >= '.$regtime.' and (b.status = 1 or b.status is null)';

      
        $firstmes =M()->query("
               select top 1 a.id,b.mid,a.module,(case when b.mid is null or b.mid = '' then 0 else 1 end ) as unread,
               (case when b.mid is null then 1 else 2 end) as readtype,a.outline,a.regtime,a.title from 
               (SELECT id,module,title,outline,regtime FROM mt_messages WHERE ( module = 2 or module = 4 ) AND ( type = 1 ) AND ( status = 1 )
               UNION ALL select id,module,title,outline,regtime from mt_messages WHERE (userall like '%,".$uid.",%') AND ( `module` = 0 ) AND ( type = 1 ) AND ( status = 1 )
               ) as a left join mt_user_messages as b on b.mid=a.id and b.uid =".$uid." ".$where ."and b.mid is null order by a.regtime desc");

        $firstmes[0]['outline'] = str_replace("{{name}}",$realname,$firstmes[0]['outline']);
        $firstmes[0]['title'] = str_replace("{{name}}",$realname,$firstmes[0]['title']);
         
        $this->assign('firstmes',$firstmes);
        
        $mes =M()->query("
               select top 5 a.id,b.mid,a.module,(case when b.mid is null or b.mid = '' then 0 else 1 end ) as unread,
               (case when b.mid is null then 1 else 2 end) as readtype,a.outline,a.regtime,a.title from 
               (SELECT id,`module`,title,outline,regtime FROM mt_messages WHERE ( `module` = 2 or `module` = 4 ) AND ( type = 1 ) AND ( status = 1 )
               UNION ALL select id,`module`,title,outline,regtime from mt_messages WHERE (userall like '%,".$id.",%') AND ( `module` = 0 ) AND ( type = 1 ) AND ( status = 1 )
               ) as a left join mt_user_messages as b on b.mid=a.id and b.uid =".$id." ".$where ." order by a.regtime desc");
        
        foreach($mes as $k=>$v){
             $mes[$k]['title']= str_replace("{{name}}",$realname,$v['title']);
        }
        
        //显示数据更新日期
        $this->assign('time',date('Y-m-d'));
        
        $this->assign('data',$mes);
        
        $num = $this->getNum();
        $this->assign('num',$num['unread']);

        $code = empty($params['code']) ? 'Spaceplus' : $params['code'];

        $this->assign('code',$code);

        $navlist = $this->getNav();
        $this->assign('navlist',$navlist);

        $importDate['supplydate'] = date('Y-m-d');
        $importDate['saledate'] = date('Y-m-d');
		
        $this->assign('cjrq',date('Y-m-d'));
        $this->assign('gyrq',date('Y-m-d'));
		
        $this->assign('importDate',$importDate);
        
        $this->assign('realname',$realname);
        $this->assign('types',$types);
        
        $this->display();
    }
    
    /**
     * @计数量
     * @return type
     */
    private function getNum(){
        //将用户SEESION赋值
        $regtime = empty($_SESSION['userInfo']['regtime'])?1487832088:$_SESSION['userInfo']['regtime'];
        $uid = empty($_SESSION['userInfo']['id'])?1:$_SESSION['userInfo']['id'];
        $realname = empty($_SESSION['userInfo']['id'])?1:$_SESSION['userInfo']['realname'];
        
        $count =M()->query("select a.id,b.mid from (SELECT id,module,title,outline,regtime FROM mt_messages WHERE ( module <> 0 ) AND ( type = 1 ) AND ( status = 1 )
           UNION ALL select id,module,title,outline,regtime from mt_messages WHERE (userall like '%,".$uid.",%') AND ( module = 0 ) AND ( type = 1 ) AND ( status = 1 ) ) 
           as a left join mt_user_messages as b on b.mid=a.id and b.uid =".$uid." where a.regtime >= ".$regtime." and (b.status is null or b.status = 1)");
        $ct1=count($count);

        $unRead =M()->query("select a.id,b.mid from (SELECT id,module,title,outline,regtime FROM mt_messages WHERE ( module <> 0 ) AND ( type = 1 ) AND ( status = 1 )
           UNION ALL select id,module,title,outline,regtime from mt_messages WHERE (userall like '%,".$uid.",%') AND ( module = 0 ) AND ( type = 1 ) AND ( status = 1 ) ) 
           as a left join mt_user_messages as b on b.mid=a.id and b.uid =".$uid." where a.regtime >= ".$regtime." and b.mid is null and (b.status = 1 or b.status is null)");

        $ct2=count($unRead);

        return array('count'=>$ct1,'unread'=>$ct2);
    }

    private function getNav()
    {
        $navlist = M()->query('select dname,code,id from mt_bdatabase WHERE status=1');

        return $navlist;
    }

    public function getrelation(){

        $data=F('Bdata/relation');
        $obj=M();
        if(empty($data)){
            $data['main']=$obj->query('select b.id as qid,qy,c.id as zid,zt,d.id as kid,a.kfs,e.id as pid,a.lpmc from mt_gx as a left join mt_region as b on a.qy=b.title left join mt_plate as  c on a.zt=c.title left join mt_company as d on a.kfs=d.title left join mt_project as e on a.lpmc=e.title');
            // $data['secondary']=

            $formats=$obj->query('select fid,b.title as ftitle,sfid,c.title as stitle from mt_formats_subformats as a inner join mt_formats as b on a.fid=b.id inner join mt_sub_formats as c on c.id=a.sfid');
            foreach($formats as $val){
                if(!isset($data['secondary'][$val['fid']])){
                    $data['secondary'][$val['fid']]=array(
                        'id'=>$val['fid'],
                        'name'=>$val['ftitle']
                    );
                }
                $data['secondary'][$val['fid']]['item'][]=array(
                    'id'=>$val['sfid'],
                    'name'=>$val['stitle']
                );
            }
            F('Bdata/relation',$data);
        }

        echo json_encode($data);
    }
}
